package com.clayecloud.lib_common.utils;

import android.util.Log;

import com.clayecloud.lib_common.apis.LoginApi;
import com.clayecloud.lib_common.apis.entity.login.ClientUser;
import com.clayecloud.lib_common.room.ClientDataBase;
import com.clayecloud.lib_common.room.entity.ClientBase;
import com.clayecloud.lib_common.room.entity.ClientToken;

import io.reactivex.Observer;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginUtil {
    /**
     *  登录工具类-实现登录更新封装
     *
     *  注意：严禁在程序未初始化前调用
     *
     */

    //登录请求的api
    private static LoginApi loginApi = HttpUtil.getInstance().create(LoginApi.class);

    /**
     * 获取用户ID
     * @return
     */
    public static int getUserID(){
        return ClientDataBase.getClientDataBaseInstance().getClientBaseService().getClientBaseByIdWithSingle(1).blockingGet().getUserID();
    }

    /**
     * 更新本地当前用户数据
     */
    public static void updateLocalUserData(){
        int userID = ClientDataBase.getClientDataBaseInstance().getClientBaseService().getClientBaseByIdWithSingle(1).blockingGet().getUserID();
        String token = ClientDataBase.getClientDataBaseInstance().getClientTokenService().getClientTokenByIdWithSingle(userID).blockingGet().getUserToken();
        //1.获取用户基础信息更新更新到缓存
        loginApi.getUserBaseInfo(token).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
            private Disposable disposable;
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                disposable = d;
            }

            @Override
            public void onNext(@androidx.annotation.NonNull MessageUtil messageUtil) {
                ClientUser clientUser = GsonUtil.getInstance().fromJson(GsonUtil.getInstance().toJson(messageUtil.getObj()), ClientUser.class);
                com.clayecloud.lib_common.room.entity.ClientUser clientBaseUser
                        = new com.clayecloud.lib_common.room.entity.ClientUser(clientUser.getUserID(),clientUser.getUserName(),clientUser.getUserState(),clientUser.getUserInfoID(),1,clientUser.getUserAvatar());
                ClientDataBase.getClientDataBaseInstance().getClientUserService().updateClientUser(clientBaseUser).subscribe(new SingleObserver<Integer>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onSuccess(@NonNull Integer integer) {
                        log(integer,"updateLocalUserData :", "基础信息更新成功");
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("updateLocalUserData","用户基础信息更新更新失败"+e.getMessage());
                        disposable.dispose();
                    }
                });
                //2.获取用户详细信息更新更新到缓存
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.e("updateLocalUserData","用户基础信息获取失败"+e.getMessage());
                disposable.dispose();
            }

            @Override
            public void onComplete() {
                disposable.dispose();
            }
        });
    }

    /**
     * 获取用户的状态
     */
    public static int getUserState(){
        //读取user的表 并返回状态
        com.clayecloud.lib_common.room.entity.ClientUser clientUser = ClientDataBase.getClientDataBaseInstance().getClientUserService().getClientUserById(getUserID()).blockingFirst();
        return clientUser.getUserLoginstate();
    }

    /**
     * 用户退出登录
     */
    public static void userLoginOut(){
        //更改用户的登录状态
        com.clayecloud.lib_common.room.entity.ClientUser clientUser = ClientDataBase.getClientDataBaseInstance().getClientUserService().getClientUserById(getUserID()).blockingFirst();
        clientUser.setUserLoginstate(0);
        ClientDataBase.getClientDataBaseInstance().getClientUserService().updateClientUser(clientUser).subscribe(new SingleObserver<Integer>() {
            private Disposable disposable;
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                disposable = d;
            }

            @Override
            public void onSuccess(@NonNull Integer integer) {
                log(integer,"userLoginOut","更改用户的登录状态");
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.e("userLoginOut","更改用户登录状态失败");
                disposable.dispose();
            }
        });
        //更新app使用用户
        ClientDataBase.getClientDataBaseInstance().getClientBaseService().updateClientBase(new ClientBase(1,0)).subscribe(new SingleObserver<Integer>() {
            private Disposable disposable;
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                disposable = d;
            }

            @Override
            public void onSuccess(@NonNull Integer integer) {
                log(integer,"userLoginOut","更改登录用户成功");
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.e("userLoginOut","用户退出登录失败");
            }
        });
    }

    /**
     * 获取本地token
     * @return
     */
    public static String getUserToken(int userID){
        ClientToken clientToken = ClientDataBase.getClientDataBaseInstance().getClientTokenService().getClientTokenById(userID).blockingFirst();
        return  clientToken.getUserToken();
    }

    /**
     * 获取用户基础信息
     * @return
     */
    public static com.clayecloud.lib_common.room.entity.ClientUser getUserBaseinfo(){
        return ClientDataBase.getClientDataBaseInstance().getClientUserService().getClientUserByIdWithSingle(getUserID()).blockingGet();
    }

    private static void log(Integer integer, String tag, String msg){
        if(integer > 0){
            Log.i(tag,msg);
        }else{
            Log.e(tag,msg);
        }
    }

}
